﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Linq.Expressions;
using ASPItalia.ModelVirtualCasting.Common;
using ASPItalia.ModelVirtualCasting.ObjectModel;
using ASPItalia.ModelVirtualCasting.Parts;

namespace ASPItalia.ModelVirtualCasting.Fake
{
    [Export("NewsRepository", typeof(INewsRepository))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    [RepositoryMetadata(Name = "NewsRepository", IsFake = true)]
    public class NewsRepository : BaseRepository<INewsRepository>, INewsRepository
    {
        protected List<News> List = new List<News>();

        public NewsRepository()
        {
            string newsBody = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a imperdiet dui. Proin erat metus, vulputate a laoreet vel, commodo sit amet nisi. Aliquam erat volutpat. Praesent leo dolor, aliquam sit amet mattis a, rhoncus a arcu. Sed augue urna, vestibulum vitae ultrices et, dictum vel leo. Suspendisse pulvinar, lectus eu volutpat molestie, nisi purus rutrum nunc, nec iaculis dolor dolor eu neque. Nam faucibus bibendum viverra. Nunc commodo tortor eu mi suscipit blandit. Curabitur egestas quam nec velit rhoncus ornare. In sit amet ante neque, et gravida tortor. In egestas nunc sit amet tortor blandit elementum. Fusce sagittis dui ut magna sagittis posuere. Ut lobortis interdum ligula in dictum. Praesent velit felis, fermentum in pharetra ut, aliquam vel felis. Donec vel urna eget ipsum adipiscing tincidunt vitae a tellus.";

            for (int i = 0; i < 10; i++)
            {
                this.List.Add(new News
                {
                    NewsTitle = string.Format("Questa è la notizia n. {0} di 10", i + 1),
                    NewsText = newsBody,
                    IsPublished = true,
                    NewsDate = new DateTime(2011, 5, 1 + i),
                    PublicationDate = new DateTime(2011, 5, 1 + i),
                    NewsID = i + 1
                });
            }

            this.List[9].NewsTitle = "Hacking news! <script type='text/javascript'>alert('Hacked!!')</script>";
        }

        public void InsertOnSubmit(News entity)
        {
        }

        public void Update(News entity)
        {
        }

        public void DeleteOnSubmit(News entity)
        {
        }

        public void SubmitChanges()
        {
        }

        public List<NewsDTO> GetLastNewsDTO(int number)
        {
            var query = from n in this.List
                        orderby n.NewsDate descending
                        select new NewsDTO
                        {
                            NewsID = n.NewsID,
                            NewsTitle = n.NewsTitle,
                            NewsText = n.NewsText,
                            PublicationDate = n.PublicationDate
                        };

            return query.Take(number).ToList();
        }

        public IQueryable<News> GetAll()
        {
            return null;
        }

        public IQueryable<News> Where(Expression<Func<News, bool>> predicate)
        {
            return null;
        }

        public long Count(Expression<Func<News, bool>> predicate)
        {
            return 0L;
        }

        public News GetByID(int id)
        {
            return this.List.Where(n => n.NewsID == id).Single();
        }

        public INewsRepository EnableLazyLoading()
        {
            return this;
        }

        public INewsRepository DisableLazyLoading()
        {
            return this;
        }

        public INewsRepository Include(string path)
        {
            return this;
        }

				public INewsRepository Include<TProperty>(Expression<Func<News, TProperty>> path)
				{
					return this;
				}

				public INewsRepository CreateQuery()
				{
					return this;
				}

				public INewsRepository DoNotTrack()
				{
					return this;
				}
		}
}
